<!doctype html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <title>添加关键字</title>
        <link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/public.css">
        <script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/jquery.global.settings.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/ajaxfileupload.js"></script>
        <script type="text/javascript" src="/webapp/js/common/metadata.js"></script>
        <script type="text/javascript" src="/webapp/js/common/config.js"></script>
    </head>
    <body>
       <div class="easyui-container-fluid">
          <div class="easyui-panel" style="border: none;">
            <div class="nav" id="nav"></div> 
            <div class="easyui-panel" style="border:none;">
              <form id="ff" method="post">
                  <div style="margin-bottom:20px;">
                      <label class="item-label"><span class="item-required">*</span>品类：</label>
                      <select class="easyui-combobox" id="categorySelect" name="categoryCode" style="width:128px;" required="required" >
                      </select>
                  </div>
                  <div>
                      <label class="item-label"><span class="item-required">*</span>关键字：</label>
                      <input name="countent" class="easyui-tagbox countent" style="width:50%;min-height:360px;"required="required" data-options="multiline:true"/>
                  </div>
                <div style="margin-left:100px;margin-botttom:20px;">
                    <a href="javascript:void(0)" class="easyui-linkbutton c8" onclick="submitForm()" style="width:80px">保存</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="cancel()" style="width:80px">取消</a>
                </div>              
              </form>     
            </div>
          </div> 
       </div>  
    </body>
    <script type="text/javascript">
        var id=getQueryParameter("id"),categoryCode=getQueryParameter("categoryCode");
        $(function(){
            setNav();
            $.when(initUserCategorySelect("categorySelect")).done(function(){initFormData()});

        });
        function initFormData(){
            if(id){
                $.ajax({
                    url:config.url +"/exclude/",
                    type:'get',
                    data:{id:id},
                    xhrFields:{withCredentials:true},
                    success:function(data){
                        //填充表单
                        if(data){
                            var formdata = changeProperty2String(data.content[0]);
                            if(!formdata.otherNames){
                                formdata.otherNames=[];
                            }
                            if(formdata.properties){
                                var props = formdata.properties;
                                for(var i in props){
                                    formdata[props[i].code] = props[i].value;
                                    if("boolean"==(typeof props[i].value)){
                                        formdata[props[i].code] = props[i].value.toString();
                                    }
                                }
                            }
                            $("#ff").form("load",formdata);
                            //model brand readonly
                            $("#categorySelect").combobox("readonly",true);
                        }
                    },
                    error:function(){

                    }
                });
            }
            $("#categorySelect").combobox("select",categoryCode);

        }
        function submitForm(){
            $("#ff").form("submit",{
                onSubmit:function(){
                    var valid = $(this).form('enableValidation').form('validate');
                    if(!valid)return false;
                    if(valid){
                        var formData = getFormData(document.getElementById("ff"));
                        formData = transfer(formData);
                        var method = "POST";
                        if(id){
                            method = "PATCH";
                            formData.id = id;
                        }
                        var bm = $(".countent").combobox('getValues');
                        formData.countent=bm;
                        $.ajax({
                            url:config.url+"/exclude",
                            type:method,
                            data:JSON.stringify(formData),
                            dataType:'json',
                            contentType:'application/json;charset=UTF-8',
                            xhrFields:{withCredentials:true},
                            beforeSend:function(){
                                $.messager.progress({
                                    title:"请稍等",
                                    interval:50,
                                    text:"操作进行中..."
                                });
                            },
                            complete:function(){
                                $.messager.progress("close");
                            },
                            success:function(data){
                                if(data.returnMessage == "YES"){
                                    $.messager.alert("提示","操作成功","info",function () {
                                        window.parent.closeCurrentTab(true);
                                    });
                                }else if(data.returnMessage == "NO"){
                                    $.messager.alert("提示","操作失败,该品类已存在","error");
                                }
                            },
                            error:function(XMLHttpRequest,textStatus,thrown){
                                $.messager.alert("提示","操作失败","error");
                            }
                        });
                        return false;
                    }
                    return false;
                }
            });
        }
        function cancel(){
            window.parent.closeCurrentTab();
            // window.location.href="/webapp/pages/product/exclude/list.html";
        }

    </script>
</html>
